package com.hongxin.nw.netty.model.analysis.info;

import com.hongxin.nw.netty.utils.BinaryTools;
import com.hongxin.nw.netty.utils.FieldAnalysis;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

//sq = 0 时使用这个类
//直流
@Slf4j
@Setter
@Getter
public class DTmmenb1{

    private String infoAdr;//信息对象地址
    private String value;//value
    private String qds;//QDS带品质描述

    private int gun;//枪口号
    private int infoAdr_i;
    private int sq;
    private int idx = 0;
    private ByteBuf byteBuf;

    private void subInfoadr() {
        if (sq == StaticValue.SQ0){
//            this.infoadr = substring(TypeIdentif.BYTE_INFO_ADDRESS.getbyteLength());
//            this.infoadr = BinaryTools.converter(this.infoadr);
//            this.gun = String.valueOf(this.infoadr.charAt(0));
//            this.infoadr = String.valueOf(Integer.parseInt(this.infoadr.substring(1),16));

            int byteLength = TypeIdentif.BYTE_INFO_ADDRESS.getInt();
            this.infoAdr = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
            this.infoAdr = BinaryTools.converter(this.infoAdr);
            this.gun = Integer.parseInt(this.infoAdr.substring(0,1));
            this.infoAdr = String.valueOf(Integer.parseInt(this.infoAdr.substring(2),16));
            this.idx += byteLength;
        }
    }
    private void subValue() {
//        this.value = substring(TypeIdentif.BYTE_VALUE.getbyteLength());
//        this.infoadr_i = Integer.parseInt(this.infoadr);

        int byteLength = TypeIdentif.BYTE_VALUE.getInt();
        this.value = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
        this.infoAdr_i = Integer.parseInt(this.infoAdr);
        this.idx += byteLength;

        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_0.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_0.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,1);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_1.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_1.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,2);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_2.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_2.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,1);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_3.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_3.getInfoMachine11Str();
            this.value = FieldAnalysis.toOct(this.value);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_4.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_4.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,1);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_5.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_5.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,1);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_6.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_6.getInfoMachine11Str();
            this.value = FieldAnalysis.analysisBin(this.value);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_7.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_7.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,3);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_8.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_8.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,3);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_9.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_9.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,1);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_10.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_10.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,1);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_11.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_11.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,1);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_12.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_12.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,2);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_13.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_13.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,2);
            return ;
        }
        if (this.infoAdr_i == TypeIdentif.INFO_MACHINE_11_14.getInt()){
            this.infoAdr = TypeIdentif.INFO_MACHINE_11_14.getInfoMachine11Str();
            this.value = FieldAnalysis.hexToDecimal(this.value,2);
            return ;
        }
    }
    private void subQds() {
//        this.qds = substring(TypeIdentif.BYTE_QDS.getbyteLength());
//        this.qds = FieldAnalysis.analysisBin(this.qds);

        int byteLength = TypeIdentif.BYTE_QDS.getInt();
        this.qds = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
        this.qds = FieldAnalysis.analysisBin(this.qds);
        this.idx += byteLength;
    }

    public int analysisStr(int idx, ByteBuf byteBuf, int sq, String infoAdr, int gun) {
        this.idx = idx;
        this.byteBuf = byteBuf;
        this.sq = sq;
        this.infoAdr = infoAdr;
        this.gun = gun;
        subInfoadr();
        subValue();
        subQds();
        return this.idx;
    }

    @Override
    public String toString() {
        return "DTmmenb1{" +
                "infoAdr_i='" + infoAdr_i + '\'' +
                ", gun='" + gun + '\'' +
                ", infoAdr='" + infoAdr + '\'' +
                ", value='" + value + '\'' +
                ", qds='" + qds + '\'' +
                '}';
    }
}